<template>
  <div>
    <a-row>
      <a-col :span="12" class="card-box">
        <a-card title="CPU">
          <div>
            <table style="width: 100%;">
              <thead>
                <tr>
                  <th><div>属性</div></th>
                  <th><div>值</div></th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td><div>核心数</div></td>
                  <td><div v-if="server.cpu">{{ server.cpu.cpuNum }}</div></td>
                </tr>
                <tr>
                  <td class="el-table__cell is-leaf"><div class="cell">用户使用率</div></td>
                  <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.used }}%</div></td>
                </tr>
                <tr>
                  <td><div class="cell">系统使用率</div></td>
                  <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.sys }}%</div></td>
                </tr>
                <tr>
                  <td class="el-table__cell is-leaf"><div class="cell">当前空闲率</div></td>
                  <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.free }}%</div></td>
                </tr>
              </tbody>
            </table>
          </div>
        </a-card>
      </a-col>

      <a-col :span="12" class="card-box">
        <a-card title="内存">
          <div>
            <table style="width: 100%;">
              <thead>
                <tr>
                  <th><div>属性</div></th>
                  <th><div>内存</div></th>
                  <th><div>JVM</div></th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td><div>总内存</div></td>
                  <td><div v-if="server.mem">{{ server.mem.total }}G</div></td>
                  <td><div v-if="server.jvm">{{ server.jvm.total }}M</div></td>
                </tr>
                <tr>
                  <td><div>已用内存</div></td>
                  <td><div v-if="server.mem">{{ server.mem.used }}G</div></td>
                  <td><div v-if="server.jvm">{{ server.jvm.used }}M</div></td>
                </tr>
                <tr>
                  <td><div>剩余内存</div></td>
                  <td><div v-if="server.mem">{{ server.mem.free }}G</div></td>
                  <td><div v-if="server.jvm">{{ server.jvm.free }}M</div></td>
                </tr>
                <tr>
                  <td><div>使用率</div></td>
                  <td><div v-if="server.mem" :class="{'text-danger': server.mem.usage > 80}">{{ server.mem.usage }}%</div></td>
                  <td><div v-if="server.jvm" :class="{'text-danger': server.jvm.usage > 80}">{{ server.jvm.usage }}%</div></td>
                </tr>
              </tbody>
            </table>
          </div>
        </a-card>
      </a-col>
      <a-col :span="24" class="card-box">
        <a-card title="服务器信息">
          <a-descriptions>
            <a-descriptions-item label="服务器名称" span="2"><div v-if="server.sys">{{ server.sys.computerName }}</div></a-descriptions-item>
            <a-descriptions-item label="操作系统"><div v-if="server.sys">{{ server.sys.osName }}</div></a-descriptions-item>
            <a-descriptions-item label="服务器IP" span="2"><div v-if="server.sys">{{ server.sys.computerIp }}</div></a-descriptions-item>
            <a-descriptions-item label="系统架构"><div v-if="server.sys">{{ server.sys.osArch }}</div></a-descriptions-item>
          </a-descriptions>
        </a-card>
      </a-col>

      <a-col :span="24" class="card-box">
        <a-card title="Java虚拟机信息">
          <a-descriptions>
            <a-descriptions-item label="Java名称" span="2"><div v-if="server.jvm">{{ server.jvm.name }}</div></a-descriptions-item>
            <a-descriptions-item label="Java版本"><div v-if="server.jvm">{{ server.jvm.version }}</div></a-descriptions-item>
            <a-descriptions-item label="启动时间" span="2"><div v-if="server.jvm">{{ server.jvm.startTime }}</div></a-descriptions-item>
            <a-descriptions-item label="运行时长"><div v-if="server.jvm">{{ server.jvm.runTime }}</div></a-descriptions-item>

            <a-descriptions-item label="安装路径" span="2"><div class="cell" v-if="server.jvm">{{ server.jvm.home }}</div></a-descriptions-item>
            <a-descriptions-item label="项目路径"><div class="cell" v-if="server.sys">{{ server.sys.userDir }}</div></a-descriptions-item>
            <a-descriptions-item label="运行参数" span="2"><div class="cell" v-if="server.jvm">{{ server.jvm.inputArgs }}</div></a-descriptions-item>
          </a-descriptions>
        </a-card>
      </a-col>
      <a-col :span="24" class="card-box">
        <a-card title="磁盘状态">
          <div>
            <table style="width: 100%;">
              <thead>
                <tr>
                  <th><div>盘符路径</div></th>
                  <th><div>文件系统</div></th>
                  <th><div>盘符类型</div></th>
                  <th><div>总大小</div></th>
                  <th><div>可用大小</div></th>
                  <th><div>已用大小</div></th>
                  <th><div>已用百分比</div></th>
                </tr>
              </thead>
              <tbody v-if="server.sysFiles">
                <tr v-for="(sysFile, index) in server.sysFiles" :key="index">
                  <td><div>{{ sysFile.dirName }}</div></td>
                  <td><div>{{ sysFile.sysTypeName }}</div></td>
                  <td><div>{{ sysFile.typeName }}</div></td>
                  <td><div>{{ sysFile.total }}</div></td>
                  <td><div>{{ sysFile.free }}</div></td>
                  <td><div>{{ sysFile.used }}</div></td>
                  <td><div :class="{'text-danger': sysFile.usage > 80}">{{ sysFile.usage }}%</div></td>
                </tr>
              </tbody>
            </table>
          </div>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>
<script>
import { getServerMonitor } from '@/api/system'
export default {
  data () {
    return {
      // 服务器信息
      server: []
    }
  },
  created () {
    this.getServerMonitor()
    this.openLoading()
  },
  methods: {
    /** 查询服务器信息 */
    getServerMonitor () {
      getServerMonitor().then(response => {
        this.server = response.data
        this.$loading.hide()
      })
    },
    // 打开加载层
    openLoading () {
      this.$loading.show({ tip: '正在加载服务监控数据，请稍候！' })
    }
  }
}
</script>
